﻿using System.Collections.Generic;
using Glue.Internals;
using GlueTests.Builders;
using Xunit;

namespace GlueTests
{
    public class EnumerableMemberTest
    {
        private readonly List<int> integers;
        private readonly object returnValue;

        public class Person { public List<int> Ints { get; set; } }
        public class AnotherPerson { public List<int> Ints { get; set; } }

        public EnumerableMemberTest()
        {
            var member = new PropertyBuilder(typeof(Person), "Ints").Build() as ICollectionMember;
            member.PassSpecialMappableCollections();
            integers = new List<int> { 1, 2 };
            var person = new Person { Ints = integers };
            returnValue = member.InvokeGet(person);
        }

        [Fact]
        public void Should_send_IGlueCollection_from_getter()
        {
            Assert.True(returnValue is IGlueCollection,"ReturnType was: " + returnValue.GetType().Name);
        }

        [Fact]
        public void Should_return_right_values_in_IGlueCollection()
        {
            var collection = (IGlueCollection)returnValue;
            Assert.True(collection.MoveNext());
            Assert.Equal(integers[0], collection.Current);
            Assert.True(collection.MoveNext());
            Assert.Equal(integers[1], collection.Current);
        }

        [Fact]
        public void Should_return_correct_count()
        {
            var collection = (IGlueCollection)returnValue;
            Assert.Equal(2, collection.Count);
        }

        [Fact]
        public void MoveNext_should_return_false_when_no_more_elements()
        {
            var collection = (IGlueCollection)returnValue;
            Assert.True(collection.MoveNext());
            Assert.True(collection.MoveNext());
            Assert.False(collection.MoveNext());
        }
    }
}
